package cn.wolfcode.mycat.config;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Jason
 * @Date: 2021/03/23/14:31
 * @Description:
 */
//@Configuration
@Slf4j
public class ElasticsearchConfig {

	@Value("${elasticsearch.nodes}")
	private String esNodes;
	
//	@Value("${elasticsearch.username}")
	private String username;
	
//	@Value("${elasticsearch.password}")
	private String password;
	
	@Bean("restHighLevelClient")
	public RestHighLevelClient getElasticsearchClient() {
		System.out.println("====="+JSONObject.toJSONString(getEsNodes()));
		RestClientBuilder restClientBuilder = RestClient.builder(getEsNodes());
		//设置连接超时和套接字超时
		restClientBuilder.setRequestConfigCallback(
				requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(10000).setConnectTimeout(60000));
		//配置HTTP异步请求ES的线程数
		/*restClientBuilder.setHttpClientConfigCallback(
				httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultIOReactorConfig(
						IOReactorConfig.custom().setIoThreadCount(1).build()));*/
		//设置监听器，每次节点失败都可以监听到，可以作额外处理
		restClientBuilder.setFailureListener(new RestClient.FailureListener() {
			@Override
			public void onFailure(Node node) {
				super.onFailure(node);
				log.error(node.getHost() + "--->elasticsearch该节点失败了");
			}
		});
		//如果配置了用户名密码需要下面这一段
		/*restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
			@Override
			public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
				CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
				credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(userName,password));
				return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
			}
		});*/
		RestHighLevelClient client = new RestHighLevelClient(restClientBuilder);
		return client;
	}
	
	private HttpHost[] getEsNodes() {
		if (StringUtils.isEmpty(esNodes)) {
			log.error("elasticsearch nodes未配置,创建es客户端失败");
			return null;
		}
		return Arrays.stream(esNodes.split(",")).map(HttpHost::create).filter(Objects::nonNull).toArray(HttpHost[]::new);
	}
}
